package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.LogApi;
import com.tanhua.dubbo.server.api.ManageApi;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Log;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.server.vo.CommentVo;
import com.tanhua.server.vo.LogVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VideosVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageService {

    @Reference
    private ManageApi manageApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private QuanZiApi quanZiApi;

    @Reference
    private LogApi logApi;

    /**
     * 评论列表翻页
     * @param page 当前页
     * @param pagesize 每页显示条数
     * @param messageID 消息ID
     * @param sortProp 排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @return
     */
    public PageResult queryCommentList(Integer page, Integer pagesize, String messageID, String sortProp, String sortOrder) {
        //通过调用dubbo服务,根据当前的消息id查询评论列表
        List<Comment> commentList= manageApi.queryCommentList(page,pagesize,messageID,sortProp,sortOrder);
        //遍历集合封装数据
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            //根据用户id查询用户详细信息
            LambdaQueryWrapper<UserInfo> query = new LambdaQueryWrapper<>();
            query.eq(UserInfo::getUserId, comment.getUserId());
            UserInfo userInfo = userInfoMapper.selectOne(query);
            //封装对象
            CommentVo commentVo = new CommentVo();
            commentVo.setId(StrUtil.toString(comment.getId()));
            commentVo.setNickname(userInfo.getNickName());
            commentVo.setUserId(Convert.toInt(comment.getUserId()));
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(Convert.toLong(comment.getCreated()));
            commentVoList.add(commentVo);
        }

        //将集合添加到PageRequest返回前端
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setItems(commentVoList);
        pageResult.setCounts(Convert.toInt(quanZiApi.queryCommentCount(messageID)));
        return pageResult;
    }


    /**
     * 视频记录翻页
     * @param page 当前页
     * @param pagesize 每页显示条数
     * @param uid 当前发布视频的用户id
     * @param sortProp 排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @return
     */
    public PageResult queryVideosList(Integer page, Integer pagesize, Long uid, String sortProp, String sortOrder) {
        //通过dubbo服务,根据用户id查询视频列表
        List<Video> videoList=manageApi.queryVideosList(page,pagesize,uid,sortProp,sortOrder);
        //遍历集合封装数据
        List<VideosVo> videosVoList = new ArrayList<>();
        for (Video video : videoList) {
            //根据用户id查询用户详细信息
            LambdaQueryWrapper<UserInfo> query = new LambdaQueryWrapper<>();
            query.eq(UserInfo::getUserId, video.getUserId());
            UserInfo userInfo = userInfoMapper.selectOne(query);
            //创建vo对象封装数据
            VideosVo videosVo = new VideosVo();
            videosVo.setId(Convert.toInt(video.getVid()));
            videosVo.setUserId(Convert.toInt(video.getUserId()));
            videosVo.setNickname(userInfo.getNickName());
            videosVo.setCreateDate(Convert.toLong(video.getCreated()));
            videosVo.setVideoUrl(video.getVideoUrl());
            videosVo.setPicUrl(video.getPicUrl());
            videosVo.setReportCount(0); //举报数
            videosVo.setLikeCount(Convert.toInt(quanZiApi.queryLikeCount(Convert.toStr(video.getId()))));  //点赞数
            videosVo.setCommentCount(Convert.toInt(quanZiApi.queryCommentCount(Convert.toStr(video.getId()))));  //评论数
            videosVo.setForwardingCount(0); //转发数
            videosVoList.add(videosVo);
        }

        //封装PageResult返回页面
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setItems(videosVoList);
        return pageResult;
    }

    /**
     * 日志翻页
     * @param page 当前页
     * @param pagesize 每页显示数
     * @param uid 发布动态的用户id
     * @param sortProp 排序字段
     * @param sortOrder sortOrder ascending 升序 descending 降序
     * @return
     */
    public PageResult queryLogsList(Integer page, Integer pagesize, Long uid, String sortProp, String sortOrder) {
        //通过调用dubbo服务查询日志信息
        List<Log> logList = logApi.queryLog(page, pagesize, uid, sortProp, sortOrder);
        //遍历集合封装数据
        List<LogVo> logVoList = new ArrayList<>();
        for (Log log : logList) {
            LogVo logVo = new LogVo();
            logVo.setLogTime(Convert.toLong(log.getLogTime()));
            logVo.setEquipment(log.getEquipment());
            logVo.setPlace(log.getPlace());
            logVoList.add(logVo);
        }

        //封装vo对象返回前端
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPage(pagesize);
        pageResult.setItems(logVoList);
        return pageResult;

    }
}
